package com.ruoyi.web.controller.threeViolation;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.utils.pengjie.SnowflakeIdUtils;
import com.ruoyi.threeViolation.domain.ViolationEducating;
import com.ruoyi.threeViolation.vo.ViolationTemplateVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.threeViolation.domain.ViolationTemplate;
import com.ruoyi.threeViolation.service.IViolationTemplateService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 三违模板管理Controller
 *
 * @author lth
 * @date 2023-12-18
 */
@RestController
@RequestMapping("/threeViolation/template")
public class ViolationTemplateController extends BaseController
{
    @Autowired
    private IViolationTemplateService violationTemplateService;

    /**
     * 查询三违模板管理列表
     */
    @PreAuthorize("@ss.hasPermi('threeViolation:template:list')")
    @GetMapping("/list")
    public TableDataInfo list(ViolationTemplate violationTemplate)
    {
        startPage();
        List<ViolationTemplate> list = violationTemplateService.selectViolationTemplateList(violationTemplate);
        return getDataTable(list);
    }

    /**
     * 导出三违模板管理列表
     */
    @PreAuthorize("@ss.hasPermi('threeViolation:template:export')")
    @Log(title = "三违模板管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ViolationTemplate violationTemplate)
    {
        List<ViolationTemplate> list = violationTemplateService.selectViolationTemplateList(violationTemplate);
        ExcelUtil<ViolationTemplate> util = new ExcelUtil<ViolationTemplate>(ViolationTemplate.class);
        util.exportExcel(response, list, "三违模板管理数据");
    }

    /**
     * 获取三违模板管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('threeViolation:template:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(violationTemplateService.selectViolationTemplateById(id));
    }

    /**
     * 新增三违模板管理
     */
    @PreAuthorize("@ss.hasPermi('threeViolation:template:add')")
    @Log(title = "三违模板管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ViolationTemplate violationTemplate)
    {
        long id = new SnowflakeIdUtils(5, 8).nextId();
        violationTemplate.setId(String.valueOf(id));
        String s = "SW";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        String currentDate = LocalDate.now().format(formatter);
        String code = s + currentDate;
        violationTemplate.setTemplateCode(code);
        return toAjax(violationTemplateService.insertViolationTemplate(violationTemplate));
    }

    /**
     * 修改三违模板管理
     */
    @PreAuthorize("@ss.hasPermi('threeViolation:template:edit')")
    @Log(title = "三违模板管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ViolationTemplate violationTemplate)
    {
        return toAjax(violationTemplateService.updateViolationTemplate(violationTemplate));
    }

    /**
     * 删除三违模板管理
     */
    @PreAuthorize("@ss.hasPermi('threeViolation:template:remove')")
    @Log(title = "三违模板管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(violationTemplateService.deleteViolationTemplateByIds(ids));
    }


    /**
     *      分页查询
     * 查询三违volation_category volation_level列表
     */
    @PreAuthorize("@ss.hasPermi('threeViolation:educating:query')")
    @GetMapping("/getCategoryList")
    public TableDataInfo getCategoryList(@RequestBody ViolationTemplateVo vo)
    {
        PageHelper.startPage(vo.getPageNum(),vo.getPageSize());
        List<ViolationTemplate> list = violationTemplateService.getCategoryList(vo);
        return getDataTable(list);
    }


}
